<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" ></meta>
<#include "../common.html">
</head>
<body>
<div  class="easyui-panel" data-options="fit:true,border:false">
	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'north',title:'条件过滤',collapsed:false" style="height:80px;overflow: hidden;">
			 <form id="searchUserForm" style="margin-top: 10px;">
			<table>
			
				<tr>
					 <td>用户名</td><td><input type="text" name="username"/></td>
					<td>状态</td><td><input type="text" name="status" id="status-combobox" class="easyui-combobox"/></td>
					<td>
						<a  href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="searchUser()">过滤</a> 
					</td>
				</tr>
			
			</table>
			</form>
			
		</div>  
    	<div data-options="region:'center'">  
        	<table id="userGrid"></table>
    	</div> 
	</div>
	
	
	
</div>
	<script>
    var userGrid;
	$(document).ready(function(){
		var statusData = [{text:'正常',value:'1'},{text:'禁用',value:'0'},{text:'待审核',value:'5'},{text:'其他',value:'6'}];
		$("#status-combobox").combobox({
			data:statusData,
			textField:'text',
			valueField:'value',
			panelHeight:'auto'
		});

        userGrid = $('#userGrid').datagrid($.extend({
		    	url : '/easyui/user/list',
		    	remoteSort : true,
				singleSelect : true,
				rownumbers : true,
		    	/* frozenColumns : [
		    		[
		    		 {field : ' ', title: ' ', checkbox : true}
		    		]
		    	], */
				sortName:'gmtModified desc,gmtCreate',
				sortOrder:'desc',
				columns : [
					[
					 	{field : 'id', title : 'ID',align:'center',width:120},
		    		 	{field : 'username', title : '用户名',align:'center',width:120,sortable:true},
					 	{field :'nickname', title :'昵称', width : 120,align:'center'},
				        {field : 'email', title : '邮箱', width : 200,align:'center'},
				        {field : 'status', title : '状态', width : 120,align:'center',
				       		formatter : function(value, row, index){
				       			if(value ==1){
				       				return '正常';
				       			}else if(value==0){
				       				return '禁用';
				       			}else if (value == 5) {
									return '待审核';
								}else{
				       				return "其他";
								}
				       		}	
				        },
				        {field : 'gmtCreate', title : '创建时间', width : 200, formatter:formatDatebox,align:'center'},
						{field : 'gmtModified', title : '修改时间', width : 200,align:'center', formatter:formatDatebox}
					]
				]
				<@shiro.hasRole name="2">
					,toolbar : [
							{
								iconCls : 'icon-add',
								text : '添加',
								handler : function(){
									showAddUserDialog();
								}
							},

						{
					iconCls : 'icon-edit',
					text : '修改',
					handler : function(){	
						var checkUser = userGrid.datagrid('getSelections');//选择的用户
                        if(checkUser && 1 == checkUser.length){
                        	showModifyUserDialog(checkUser[0]);
                        }else{
                            parent.$.messager.alert('错误', '请选择一个用户', 'error');
                        }
					}
				},{
					iconCls : 'icon-add',
					text : '分配角色',
					handler : function(){	
						var checkUser = userGrid.datagrid('getSelections');//选择的用户
						if(checkUser[0].status == 5) {
							parent.$.messager.alert('提示', '审核用户不能分配角色', 'info');
							return;
						}
                        if(checkUser && 1 == checkUser.length){
                        	showUserAssignRoleDialog(checkUser[0]);
                        }else{
                            parent.$.messager.alert('错误', '请选择一个用户', 'error');
                        }
					}
				} ,{
				iconCls : 'icon-edit',
				text : '审核通过',
				handler : function(){
					var checkUser = userGrid.datagrid('getSelections');//选择的用户

					if(checkUser && 1 == checkUser.length){
						if(checkUser[0].status != 5){
							parent.$.messager.alert('错误', '请选择待审核用户', 'error');
						}else {
							$.ajax({
								url: "/admin/user/optionUserStatus",
								data:  JSON.stringify(checkUser),
								contentType: "application/json; charset=utf-8",
								type: "POST",
								dataType: 'json',
								success: function(data){
									if (data.success) {
										parent.$.messager.alert('提示', data.msg, 'info');
										searchUser();
									} else {
										parent.$.messager.alert('错误', data.msg, 'error');
									}
								}
							});
						}
					}else{
						parent.$.messager.alert('错误', '请选择一个用户', 'error');
					}
				}
			} ,{
					iconCls : 'icon-remove',
					text : '删除',
					handler : function(){
						var checkUser = userGrid.datagrid('getSelections');//选择的用户

                        if(checkUser && checkUser.length == 1){
                        	deleteCheckUser(checkUser[0]);
                        }else{
                            parent.$.messager.alert('错误', '请选择一个用户', 'error');
                        }
					}
				}]
				</@shiro.hasRole>
		    },ns.datagridOptions));
	});
	
	function showAddUserDialog(){
	   var userDialog =  parent.ns.modalDialog({
           title : '添加',
           width : 470,
           height : 370,
           resizable : true,
           url : '/easyui/user/add',
           buttons : [{
               text : '保存',
               iconCls : 'icon-save',
               handler : function(){
                   userDialog.find('iframe').get(0).contentWindow.submitUser(userDialog,userGrid,parent.$);
               }
           },{
				text : '关闭',
				iconCls : 'icon-cancel',
				handler : function() {
					userDialog.dialog('close');
				}
			}]

       });
	}
	
	function showModifyUserDialog(select){
		
		 var userDialog =  parent.ns.modalDialog({
	           title : '修改',
	           width : 470,
	           height : 370,
	           resizable : true,
	           url : '/easyui/user/modify?username='+select.username+"&nickname="+select.nickname+"&email="+select.email+"&status="+select.status+"&id="+select.id,
	           buttons : [{
	               text : '保存',
	               iconCls : 'icon-save',
	               handler : function(){
	                   userDialog.find('iframe').get(0).contentWindow.submitUser(userDialog,userGrid,parent.$);
	               }
	           },{
					text : '关闭',
					iconCls : 'icon-cancel',
					handler : function() {
						userDialog.dialog('close');
					}
				}]

	       });
	}
	
	function showUserAssignRoleDialog(select){
		var roleDialog =  parent.ns.modalDialog({
	           title : '分配角色',
	           width : 400,
	           height : 400,
	           resizable : true,
	           url : '/easyui/user/roleSet?id='+select.id,
	           buttons : [{
	               text : '保存',
	               iconCls : 'icon-save',
	               handler : function(){
					   roleDialog.find('iframe').get(0).contentWindow.submitRole(roleDialog,parent.$);
	               }
	           },{
					text : '关闭',
					iconCls : 'icon-cancel',
					handler : function() {
						roleDialog.dialog('close');
					}
				}]

	       });
	}

	function deleteCheckUser(checkUser){
		
		$.messager.confirm('确认','确认删除用户?',function(r){  
    	    if (r){
				$.ajax({
					url: "/admin/user/delete",
					data:  {"id": checkUser.id},
					type: "POST",
					dataType: 'json',
					success: function(result){
						if(result.success){
							userGrid.datagrid('clearSelections').datagrid('clearChecked');
							userGrid.datagrid('reload');
						}else{
							$.messager.alert('错误','删除用户失败','error');
						}
					}
				});
    	    }  
    	}); 
	}
	
	//搜索用户
	function searchUser(){
		 var form= $("#searchUserForm");
		 var data = ns.serializeObject(form);
		 userGrid.datagrid('load',data);
	}
	</script>
	</body>
</html>